#include"StdAfx.h"


pgMaterial *pgOBJMaterialLoader::LoadMaterial(const char* szFilePath)
{
	PG_LOG_INFO_EX(TOPIC_OBJLOADER, "Loading material from file: %s!", szFilePath);
	pgFile matFile;


	if (!matFile.OpenFile(szFilePath))
	{
		return NULL;
	}
	

	pgMaterial *pMat = new NEW_MEM_LEAK pgMaterial();
	// Diffuse
	size_t pos = matFile.GetTextBuffer().find("\nKd");
	size_t posEnd = matFile.GetTextBuffer().find("\n", ++pos);

	pMat->SetDiffuse(ExtractVectorFromLine(matFile.GetTextBuffer().substr(pos, posEnd - pos)));

	PG_LOG_INFO_EX(TOPIC_OBJLOADER, "Finished loading material from file: %s!", szFilePath);

	return pMat;
}




XMFLOAT3 pgOBJMaterialLoader::ExtractVectorFromLine(std::string &sLine)
{
	size_t pos1 = sLine.find_first_of(" ");
	size_t pos2 = sLine.find(" ", ++pos1);
	size_t pos3 = sLine.find(" ", ++pos2);

	XMFLOAT3 result;

	result.x = atof(sLine.substr(pos1, pos2 - pos1).c_str());
	result.y = atof(sLine.substr(pos2, pos3 - pos2).c_str());
	result.z = atof(sLine.substr(pos3, sLine.length() - pos3).c_str());

	return result;
}